		include	Common.asm

; ==========================================================
Stack		segment para stack 'stack'
	dw	256 dup (0)
Stack		ends


; ==========================================================
DataSeg		segment para public 'data'
	wa	word 0
	wb	word 0
	wc	word 0
	wd	word 0

	terminated	byte 0
		
	TestWndClass	TWndClass { DoNothing, DoNothing,
		TstOnDraw, DoNothing, 
		TstOnKey, TstOnMouse }

DataSeg		ends


; ==========================================================
CodeSeg		segment para public 'code'
		assume	ds: DataSeg

Main		proc	near

	mov	ax, DataSeg
	mov	ds, ax

	call	AllocWndData
	mov	[bx].TWndData.Color, 1
	mov	[bx].TWndData.WndClass, offset TestWndClass
	mov	[bx].TWndData.X, 30
	mov	[bx].TWndData.Y, 20
	mov	[bx].TWndData.SizeX, 200
	mov	[bx].TWndData.SizeY, 150
	mov	wa, bx

	call	AllocWndData
	mov	[bx].TWndData.Color, 2
	mov	[bx].TWndData.WndClass, offset TestWndClass
	mov	[bx].TWndData.X, 80
	mov	[bx].TWndData.Y, 20
	mov	[bx].TWndData.SizeX, 100
	mov	[bx].TWndData.SizeY, 100
	mov	wb, bx

	call	AllocWndData
	mov	[bx].TWndData.Color, 3
	mov	[bx].TWndData.WndClass, offset TestWndClass
	mov	[bx].TWndData.X, 10
	mov	[bx].TWndData.Y, 60
	mov	[bx].TWndData.SizeX, 100
	mov	[bx].TWndData.SizeY, 100
	mov	wc, bx

	call	AllocWndData
	mov	[bx].TWndData.Color, 7
	mov	[bx].TWndData.WndClass, offset TestWndClass
	mov	[bx].TWndData.X, 20
	mov	[bx].TWndData.Y, 10
	mov	[bx].TWndData.SizeX, 10
	mov	[bx].TWndData.SizeY, 10
	mov	wd, bx


	mov	si, wa
	mov	di, 0
	call	AddChildWnd

	mov	si, wb
	mov	di, wa
	call	AddChildWnd

	mov	si, wc
	mov	di, wa
	call	AddChildWnd

	mov	si, wd
	mov	di, wb
	call	AddChildWnd

	; Set Video mode - Display Mode
	mov	ah, 0h		; ah = 0h - Set Video mode
	mov	al, 13h		; al = 13h - 320x200x256
	int	10h

;		call	ShowMouseCursor

MainLoop:
	mov	GraphOriginX, 0
	mov	GraphOriginY, 0

	mov	bx, 0
	push	bx
	push	bx
	mov	bx, GraphScreenWidth
	push	bx
	mov	bx, GraphScreenHeight
	push	bx
	mov	bx, 0
	push	bx
	call	FillRect

	call	UIDraw
;		call	UIProcess
	call	UIKey
	call	UIMouse

	call	GetMouseState
	mov	EventData.ButtonState, bx
	mov	EventData.MouseX, cx
	mov	EventData.MouseY, dx

	push	cx
	push	dx
	mov	bx, 2
	push	bx
	push	bx
	push	ax
	call	FillRect
	inc	ax

	call	Present

	cmp	terminated, 0
	je	MainLoop

;		call	HideMouseCursor

	mov	ah, 0h		; ah = 0h - Set Video mode
	mov	al, 3h		; al = 3h - Text Mode
	int	10h

	mov	ax, 4c00h	; ah = 4ch - Terminate program, al = Exit code
	int	21h

Main		endp


TstOnDraw	proc near

	push	ax
	mov	ax, 0
	push	ax
	push	ax
	push	[si].TWndData.SizeX
	push	[si].TWndData.SizeY
	mov	ah, 0
	mov	al, [si].TWndData.Color
	push	ax
	call	FillRect
	pop	ax

	ret

TstOnDraw	endp


TstOnMouse	proc near

	call	DefMovingWndOnMouse
	call	DefWndOnMouse

	ret

TstOnMouse	endp


TstOnKey	proc

	cmp	EventData.EventType, EvtKeyPressed
	jne	SkipTstKeyPressed

	cmp	EventData.KeyChar, 'q'
	je	QKeyPressed
	cmp	EventData.KeyChar, 'Q'
	je	QKeyPressed
	cmp	EventData.KeyCode, 1
	je	QKeyPressed

	jmp	SkipTstKeyPressed

QKeyPressed:
	mov	terminated, 1
	inc	[si].TWndData.Color

SkipTstKeyPressed:

	ret

TstOnKey	endp


TstPrint	proc near

		push	ax
		push	dx
		
		mov	ah, 2h
		mov	dl, [si].TWndData.State
		int	21h
		
		pop	dx
		pop	ax
		ret

TstPrint	endp


CodeSeg		ends

		end	Main

